//
//  MovieViewController.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/17.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieCell.h"

@interface MovieViewController ()


@end


@implementation MovieViewController

#pragma mark - mainViewcontroller
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      self.navigationItem.title = @"电影";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createNav];
    [self _createMovieTableView];
    [self _createPosterView];
    
    [self _loadData];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - action
- (void)btnAction:(UIButton *)btn{
    
    UIView *flipView = self.navigationItem.rightBarButtonItem.customView;
    
    UIButton *btn1 = (UIButton *)[flipView viewWithTag:1];
    UIButton *btn2 = (UIButton *)[flipView viewWithTag:2];
    
    [self _flipView:flipView isLeft:btn2.hidden];
    [self _flipView:self.view isLeft:btn2.hidden];

    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    
    PosterView.hidden = !PosterView.hidden;
    movieTableView.hidden = !movieTableView.hidden;
    
}

#pragma mark - tools
- (void)_flipView:(UIView *)flipView isLeft:(BOOL)isLeft{
    UIViewAnimationOptions option = isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    [UIView transitionWithView:flipView duration:.3 options:option animations:nil completion:nil];
}

#pragma mark - createSubviews

-(void)_createNav{
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.translucent = YES;
    //只用于scrillowView
 //   self.automaticallyAdjustsScrollViewInsets = YES;
    
 //   self.edgesForExtendedLayout = UIRectEdgeNone;
    UIView *flipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    btn1.tag = 1;
    [btn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn1 sizeToFit];
    [flipView addSubview:btn1];
    
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    btn2.tag = 2;
    [btn2 sizeToFit];
    [btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    [flipView addSubview:btn2];
    btn2.hidden = YES;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:flipView];
    self.navigationItem.rightBarButtonItem = item;

}

- (void)_createMovieTableView{
    movieTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, Kheight) style:UITableViewStylePlain];
    movieTableView.delegate = self;
    movieTableView.dataSource = self;
    movieTableView.backgroundColor = [UIColor blackColor];
    UINib *nib = [UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    [movieTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:movieTableView];
    movieTableView.hidden = YES;
}


- (void)_createPosterView{
    PosterView = [[posterView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, Kheight)];
    PosterView.backgroundColor = [UIColor blueColor];
   
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(Kwidth, Kheight-100);
    layout.minimumInteritemSpacing = 20;
    
//    _collectionView = [[movieCollectionView alloc] initWithFrame:CGRectMake(20, 70, Kwidth, Kheight-100) collectionViewLayout:layout];
//    _imageName = [[NSMutableArray alloc] init];
//    for ( int i = 0; i < 28; i++) {
////        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, Kwidth, Kheight-100)];
//        NSString *imageName1 = [NSString stringWithFormat:@"%d@2x",i+1];
//       // imageView.image = [UIImage imageNamed:imageName];
//        [_imageName addObject:imageName1];
//    }
//    _collectionView.imageName = _imageName;
    
    
    [self.view addSubview:PosterView];
//    [PosterView addSubview:_collectionView];
    PosterView.hidden = NO;
}

#pragma mark - loadData

- (void)_loadData{
    
    messageArray = [[NSMutableArray alloc] init];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"us_box.json" ofType:nil];
    //nsdata直接存储二进制数据
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    //直接把文件里面的数据解析放到 数组或字典中
    NSDictionary *dic =  [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  
    NSArray *movieArray = dic[@"subjects"];
//
//    for (int i = 0; i < 11; i ++) {
//       
//        messageModal *modal = [[messageModal alloc] init];
//        
//        modal.title = movieArray[i][@"subject"][@"title"];
//        modal.year =  movieArray[i][@"subject"][@"year"];
//        NSNumber *number = movieArray[i][@"subject"][@"rating"][@"average"];
//        modal.average = [number floatValue];
//        modal.images = movieArray[i][@"subject"][@"images"];
//        [messageArray addObject:modal];
//    }
       for (NSDictionary *dic in movieArray) {
           NSDictionary *subject = [dic objectForKey:@"subject"];
           messageModal *modal = [[messageModal alloc] init];
           modal.title = subject[@"title"];
           modal.year = subject[@"year"];
           NSNumber *number = subject[@"rating"][@"average"];
           modal.average = [number floatValue];
           modal.images = subject[@"images"];
           [messageArray addObject:modal];
    
    }
    PosterView.movieModalArray = messageArray;
}


#pragma mark - UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [messageArray count];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    messageModal *modal = messageArray[indexPath.row];
    cell.modal = modal;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
